package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.SResource;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源定义类-[表名: 资源定义(s_resource)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class SResourceDao {

	/* 连接对象 */
	Connection conn = null;	

	public SResourceDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertSResource(SResource sResource) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into s_resource ( "
                + "resourceid,cnname,systempk,url,parentid,tablename,procid,orderid,memo,icon,orgid"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, sResource.getResourceid());
            ps.setObject(2, sResource.getCnname());
            ps.setObject(3, sResource.getSystempk());
            ps.setObject(4, sResource.getUrl());
            ps.setObject(5, sResource.getParentid());
            ps.setObject(6, sResource.getTablename());
            ps.setObject(7, sResource.getProcid());
            ps.setObject(8, sResource.getOrderid());
            ps.setObject(9, sResource.getMemo());
            ps.setObject(10, sResource.getIcon());
            ps.setObject(11, sResource.getOrgid());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert SResource is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(SResource sResource) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from s_resource where 1=1"
                + " AND resourceid = ? "
                );
            ps.setObject(1, sResource.getResourceid());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete SResource is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(SResource sResource) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update s_resource set "
                + " resourceid = ? ,"
                + " cnname = ? ,"
                + " systempk = ? ,"
                + " url = ? ,"
                + " parentid = ? ,"
                + " tablename = ? ,"
                + " procid = ? ,"
                + " orderid = ? ,"
                + " memo = ? ,"
                + " icon = ? ,"
                + " orgid = ? "
                + " where 1=1"
                + " AND resourceid = ? "
                );
            ps.setObject(1, sResource.getResourceid());
            ps.setObject(2, sResource.getCnname());
            ps.setObject(3, sResource.getSystempk());
            ps.setObject(4, sResource.getUrl());
            ps.setObject(5, sResource.getParentid());
            ps.setObject(6, sResource.getTablename());
            ps.setObject(7, sResource.getProcid());
            ps.setObject(8, sResource.getOrderid());
            ps.setObject(9, sResource.getMemo());
            ps.setObject(10, sResource.getIcon());
            ps.setObject(11, sResource.getOrgid());
            ps.setObject(12, sResource.getResourceid());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update SResource is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public SResource queryByPk(SResource sResource) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        SResource result = null;
        try {
            ps = conn.prepareStatement("select resourceid,cnname,systempk,url,parentid,tablename,procid,orderid,memo,icon,orgidfrom s_resource where 1=1 "
                + " AND resourceid = ? "
                );
            ps.setObject(1, sResource.getResourceid());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new SResource();
				result.setResourceid(rs.getString("resourceid"));
				result.setCnname(rs.getString("cnname"));
				result.setSystempk(rs.getString("systempk"));
				result.setUrl(rs.getString("url"));
				result.setParentid(rs.getString("parentid"));
				result.setTablename(rs.getString("tablename"));
				result.setProcid(rs.getString("procid"));
				result.setOrderid(rs.getString("orderid"));
				result.setMemo(rs.getString("memo"));
				result.setIcon(rs.getString("icon"));
				result.setOrgid(rs.getString("orgid"));
			}
        } catch (SQLException e) {
            throw new SQLException("update SResource is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 sResource 参数列表查询 不分页
	* @return int
	*/
    public List<SResource> queryAll(SResource sResource) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<SResource> result = new ArrayList<>();
        try {
            String sql = "select resourceid,cnname,systempk,url,parentid,tablename,procid,orderid,memo,icon,orgidfrom s_resource where 1=1 ";
                if (sResource.getResourceid() != null) {
                    sql = sql + " AND resourceid =  '" + sResource.getResourceid() + "'";
                }
                if (sResource.getCnname() != null) {
                    sql = sql + " AND cnname =  '" + sResource.getCnname() + "'";
                }
                if (sResource.getSystempk() != null) {
                    sql = sql + " AND systempk =  '" + sResource.getSystempk() + "'";
                }
                if (sResource.getUrl() != null) {
                    sql = sql + " AND url =  '" + sResource.getUrl() + "'";
                }
                if (sResource.getParentid() != null) {
                    sql = sql + " AND parentid =  '" + sResource.getParentid() + "'";
                }
                if (sResource.getTablename() != null) {
                    sql = sql + " AND tablename =  '" + sResource.getTablename() + "'";
                }
                if (sResource.getProcid() != null) {
                    sql = sql + " AND procid =  '" + sResource.getProcid() + "'";
                }
                if (sResource.getOrderid() != null) {
                    sql = sql + " AND orderid =  '" + sResource.getOrderid() + "'";
                }
                if (sResource.getMemo() != null) {
                    sql = sql + " AND memo =  '" + sResource.getMemo() + "'";
                }
                if (sResource.getIcon() != null) {
                    sql = sql + " AND icon =  '" + sResource.getIcon() + "'";
                }
                if (sResource.getOrgid() != null) {
                    sql = sql + " AND orgid =  '" + sResource.getOrgid() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				SResource obj = new SResource();
				obj.setResourceid(rs.getString("resourceid"));
				obj.setCnname(rs.getString("cnname"));
				obj.setSystempk(rs.getString("systempk"));
				obj.setUrl(rs.getString("url"));
				obj.setParentid(rs.getString("parentid"));
				obj.setTablename(rs.getString("tablename"));
				obj.setProcid(rs.getString("procid"));
				obj.setOrderid(rs.getString("orderid"));
				obj.setMemo(rs.getString("memo"));
				obj.setIcon(rs.getString("icon"));
				obj.setOrgid(rs.getString("orgid"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update SResource is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
